import 'package:flutter/material.dart';
import 'package:google_mlkit_pose_detection/google_mlkit_pose_detection.dart';
import 'package:mediapipe_task_vision/mediapipe_task_vision.dart';
import 'package:mediapipe_task_vision_example/footstep/footstep.dart';
import 'package:mediapipe_task_vision_example/footstep/footstep2.dart';


class PosePainter extends CustomPainter {
  final lankmarks;
  final width;
  final height;

  PosePainter(this.lankmarks, this.width, this.height);

  @override
  void paint(Canvas canvas, Size size) {
    double scale = size.width / width;
    var pointPaint = Paint()
      ..strokeCap = StrokeCap.butt
      ..isAntiAlias = true
      ..color = Colors.orange
      ..strokeWidth = 3.0;
    var linePaint = Paint()
      ..strokeCap = StrokeCap.butt
      ..color = Colors.white
      ..strokeWidth = 2.0;
    for (int i = 0; i < lankmarks.length - 1; i++) {
      canvas.drawCircle(
          Offset(lankmarks[i]["x"] * width * scale,
              lankmarks[i]["y"] * height * scale),
          2,
          pointPaint);
      for (Connection poseLandmark in POSE_LANDMARKS) {
        canvas.drawLine(
            Offset(lankmarks[poseLandmark.start]["x"] * width * scale,
                lankmarks[poseLandmark.start]["y"] * height * scale),
            Offset(lankmarks[poseLandmark.end]["x"] * width * scale,
                lankmarks[poseLandmark.end]["y"] * height * scale),
            linePaint);
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

class DDRPainter extends CustomPainter {
  final lankmarks;
  final width;
  final height;
  final currentStep;
  final stepAdapter;

  DDRPainter(this.lankmarks, this.width, this.height, this.currentStep,
      this.stepAdapter);

  @override
  void paint(Canvas canvas, Size size) {
    double scale = size.width / width;
    var pointPaint = Paint()
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..color = Colors.orange;
    var linePaint = Paint()
      ..strokeCap = StrokeCap.butt
      ..color = Colors.white
      ..strokeWidth = 2.0;

    for (int i = 0; i < stepAdapter.steps.length; i++) {
      StepPosition stepPosition = stepAdapter.steps[i];
      var buttonPaint = Paint()
        ..strokeCap = StrokeCap.butt
        ..color = Colors.red
            .withOpacity(currentStep == stepPosition.stepType ? 1 : 0.5)
        ..strokeWidth = 2.0;
      // canvas.drawRect(
      //     Rect.fromLTWH(
      //         width * scale * stepPosition.x - stepAdapter.rect_wh* width/2,
      //         height * scale * stepPosition.y - stepAdapter.rect_wh* width/2,
      //         stepAdapter.rect_wh* width,
      //         stepAdapter.rect_wh* width),
      //     buttonPaint);
      canvas.drawCircle(
          Offset(width * scale * stepPosition.x,
              height * scale * stepPosition.y),
          currentStep == stepPosition.stepType ? 20.0 : 10.0,
          buttonPaint);
    }

    for (int i = 0; i < lankmarks.length - 1; i++) {
      for (Connection poseLandmark in POSE_LANDMARKS) {
        canvas.drawCircle(
            Offset(lankmarks[i]["x"]  * scale,
                lankmarks[i]["y"]  * scale),
            5,
            pointPaint);
        canvas.drawLine(
            Offset(lankmarks[poseLandmark.start]["x"] *  scale,
                lankmarks[poseLandmark.start]["y"] *  scale),
            Offset(lankmarks[poseLandmark.end]["x"] *  scale,
                lankmarks[poseLandmark.end]["y"] *  scale),
            linePaint);
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}



class DDRPainter2 extends CustomPainter {
  Map<PoseLandmarkType, PoseLandmark> lankmarks;
  final width;
  final height;
  final currentStep;
  final stepAdapter;

  DDRPainter2(this.lankmarks, this.width, this.height, this.currentStep,
      this.stepAdapter);

  @override
  void paint(Canvas canvas, Size size) {
    double scale = size.width / height;
    var pointPaint = Paint()
      ..strokeCap = StrokeCap.round
      ..isAntiAlias = true
      ..color = Colors.orange;
    var linePaint = Paint()
      ..strokeCap = StrokeCap.butt
      ..color = Colors.white
      ..strokeWidth = 2.0;

    // for (int i = 0; i < stepAdapter.steps.length; i++) {
    //   StepPosition stepPosition = stepAdapter.steps[i];
    //   var buttonPaint = Paint()
    //     ..strokeCap = StrokeCap.butt
    //     ..color = Colors.red
    //         .withOpacity(currentStep == stepPosition.stepType ? 1 : 0.5)
    //     ..strokeWidth = 2.0;
    //   // canvas.drawRect(
    //   //     Rect.fromLTWH(
    //   //         width * scale * stepPosition.x - stepAdapter.rect_wh* width/2,
    //   //         height * scale * stepPosition.y - stepAdapter.rect_wh* width/2,
    //   //         stepAdapter.rect_wh* width,
    //   //         stepAdapter.rect_wh* width),
    //   //     buttonPaint);
    //   canvas.drawCircle(
    //       Offset(width * scale * stepPosition.x,
    //           height * scale * stepPosition.y),
    //       currentStep == stepPosition.stepType ? 20.0 : 10.0,
    //       buttonPaint);
    // }
    List<PoseLandmark> lankList = lankmarks.values.toList();
    for (int i = 0; i < lankList.length - 1; i++) {
      for (Connection poseLandmark in POSE_LANDMARKS) {
        canvas.drawCircle(
            Offset(lankList[i]!.x  * scale,
                lankList[i]!.y  * scale),
            5,
            pointPaint);
        canvas.drawLine(
            Offset(lankList[poseLandmark.start!]!.x *  scale,
                lankList[poseLandmark.start!]!.y *  scale),
            Offset(lankList[poseLandmark.end!]!.x *  scale,
                lankList[poseLandmark.end!]!.y *  scale),
            linePaint);
      }
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
